#pragma once

#include<iostream>

#include <string>
#include <ctime>
#include <openssl/evp.h> // 用于密码哈希
#include <openssl/rand.h> // 用于生成随机数
#include <openssl/hmac.h> // 用于生成HMAC-SHA256签名
#include <cstring>
#include <stdexcept>
#include "User.hpp" 

#pragma once

#include <iostream>
#include <string>
#include <memory>
#include "User.hpp"

class Login {
private:
    UserMapper* userMapper;

public:
    // 构造函数，接收UserMapper实例
    Login(UserMapper* mapper) : userMapper(mapper) {}
    
    // 1. 用户登录认证（简单字符串比较）
    std::unique_ptr<User> authenticate(const std::string& email, const std::string& password) {
        try {
            // 根据邮箱获取用户
            auto user = userMapper->getUserByEmail(email);
            if (!user) {
                throw std::runtime_error("用户不存在");
            }
            
            // 直接比较密码字符串
            if (password != user->password) {
                throw std::runtime_error("密码不正确");
            }
            
            return user;
        } catch(const std::exception& e) {
            std::cerr << "登录失败: " << e.what() << std::endl;
            throw;
        }
    }
    
    // 2. 注册新用户
    int registerUser(const std::string& name, const std::string& email, 
                    const std::string& password, const std::string& phone = "", 
                    int age = 0) {
        try {
            // 检查邮箱是否已存在
            auto existingUser = userMapper->getUserByEmail(email);
            if (existingUser) {
                throw std::runtime_error("该邮箱已被注册");
            }
            
            // 创建用户对象（直接存储明文密码）
            User newUser;
            newUser.name = name;
            newUser.email = email;
            newUser.password = password; // 直接存储明文密码
            newUser.phone = phone;
            newUser.age = age;
            
            // 保存用户
            return userMapper->createUser(newUser);
        } catch(const std::exception& e) {
            std::cerr << "注册失败: " << e.what() << std::endl;
            throw;
        }
    }
    
    // 3. 密码重置
    bool resetPassword(const std::string& email, const std::string& newPassword) {
        try {
            // 获取用户
            auto user = userMapper->getUserByEmail(email);
            if (!user) {
                throw std::runtime_error("用户不存在");
            }
            
            // 更新用户密码（直接存储明文）
            user->password = newPassword;
            return userMapper->updateUser(*user);
        } catch(const std::exception& e) {
            std::cerr << "重置密码失败: " << e.what() << std::endl;
            return false;
        }
    }
    
    // 4. 修改密码
    bool changePassword(int userId, const std::string& oldPassword, const std::string& newPassword) {
        try {
            // 获取用户
            auto user = userMapper->getUserById(userId);
            if (!user) {
                throw std::runtime_error("用户不存在");
            }
            
            // 验证旧密码（直接比较字符串）
            if (oldPassword != user->password) {
                throw std::runtime_error("当前密码不正确");
            }
            
            // 更新用户密码（直接存储明文）
            user->password = newPassword;
            return userMapper->updateUser(*user);
        } catch(const std::exception& e) {
            std::cerr << "修改密码失败: " << e.what() << std::endl;
            return false;
        }
    }
};